//
//  FilterViewControllerBaseViewController.m
//  OfficeBook
//
//  Created by 朱 琳 on 15/10/4.
//  Copyright (c) 2015年 sunny. All rights reserved.
//

#import "FilterViewControllerBase.h"

//CGRect beforeContentFrame;
CGRect afterContentFrame;

@interface FilterViewControllerBase ()

@end

@implementation FilterViewControllerBase

- (void)viewDidLoad {
    [super viewDidLoad];
    self.LeftOpaueWidth = 50;
    //beforeContentFrame = CGRectMake(CGRectGetWidth(self.view.frame), 0, CGRectGetWidth(self.view.frame) - self.LeftOpaueWidth, CGRectGetHeight(self.view.frame));
    afterContentFrame = CGRectMake(self.LeftOpaueWidth, 0, CGRectGetWidth(self.view.frame) - self.LeftOpaueWidth, CGRectGetHeight(self.view.frame));
    
    self.contentView = [[UIView alloc] init];
    self.contentView.backgroundColor = [UIColor colorWithHexString:@"#ebebeb"];
    
    [self initHeaderView];
    
    UISwipeGestureRecognizer *verticalGr =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
    verticalGr.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:verticalGr];
}

-(void)initHeaderView {
    self.headerView = [[UIView alloc] init];
    self.leftBtn = [[UIButton alloc] init];
    self.rightBtn = [[UIButton alloc] init];
    self.titleLabel = [[UILabel alloc] init];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigationBar"]];
    
    [self.contentView addSubview:self.headerView];
    [self.contentView addSubview:self.leftBtn];
    [self.contentView addSubview:self.rightBtn];
    [self.contentView addSubview:self.titleLabel];
    [self.headerView addSubview:imageView];
    
    self.headerView.backgroundColor = [UIColor whiteColor];
    self.leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    self.rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.headerView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.top);
        make.left.equalTo(self.contentView.left);
        make.height.equalTo(@64);
        make.right.equalTo(self.contentView.right);
    }];
    
    [imageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.top);
        make.bottom.equalTo(self.headerView.bottom);
        make.left.equalTo(self.headerView.left);
        make.right.equalTo(self.headerView.right);
    }];
    
    [self.leftBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerView.left);
        make.centerY.equalTo(self.headerView.centerY).offset(20/2.f);
        make.width.equalTo(@70);
        make.height.equalTo(@40);
    }];
    [self.leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.leftBtn.titleLabel.font = [UIFont systemFontOfSize:15];

    [self.rightBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.headerView.right);
        make.centerY.equalTo(self.headerView.centerY).offset(20/2.f);
        make.width.equalTo(@70);
        make.height.equalTo(@40);
    }];
    [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headerView.centerX);
        make.centerY.equalTo(self.headerView.centerY).offset(20/2.f);
        make.width.equalTo(@100);
        make.height.equalTo(@40);
    }];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.titleLabel.textColor = [UIColor whiteColor];
    
    [self.leftBtn addTarget:self action:@selector(doLeftBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.rightBtn addTarget:self action:@selector(doRightBtn) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - animation
NSString *kContentViewAnimationType = @"ContentViewAnimationType";

-(void)pushContentView {
    self.contentView.frame = afterContentFrame;
    //默认push动画
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromRight;
    transition.delegate = self;
    [transition setValue:@"push" forKey:kContentViewAnimationType];
    [self.contentView.layer addAnimation:transition forKey:nil];
    [self.view addSubview:self.contentView];
    
}

-(void)popContentView {
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    transition.delegate = self;
    [transition setValue:@"pop" forKey:kContentViewAnimationType];
    [self.contentView.superview.layer addAnimation:transition forKey:nil];
    [self.contentView removeFromSuperview];
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    //dismiss controller
    NSString* value = [theAnimation valueForKey:kContentViewAnimationType];
    if (flag == YES && [value  isEqual: @"pop"]) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }
}

#pragma mark - action
-(void)doLeftBtn {
    [self popContentView];
}

-(void)doRightBtn {
    
}

//向右清扫退出
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *)uigr{
    CGPoint location = [uigr locationInView:self.view];
    if (location.x < self.LeftOpaueWidth && location.x >= 0) {
        [self popContentView];
    }
}
@end
